<?php

class TopographicMapReviewsController extends AppController {

    var $name = 'TopographicMapReviews';

    public function index($topographic_map_id) {

        $this->layout = "ajax";
        $this->set('topographic_map_id', $topographic_map_id);
        $this->set('property_id', $this->TopographicMapReview->TopographicMap->field('TopographicMap.property_id', array('TopographicMap.id' => $topographic_map_id)));
        $this->TopographicMapReview->recursive = -1;
        $this->set('revisiones', $this->TopographicMapReview->find('all', array('recursive' => 0, 'conditions' => array('TopographicMapReview.topographic_map_id' => $topographic_map_id), 'order' => array('TopographicMapReview.id DESC'), 'fields' => array('TopographicMapReview.*'))));
    }

    public function add($topographic_map_id) {
        $this->set('topographic_map_id', $topographic_map_id);
        if (!empty($this->data)) {
            if ($this->TopographicMapReview->saveAll($this->data)) {
                $this->Session->setFlash('Registro guardado correctamente');
                $this->redirect(array('controller' => 'TopographicMapReviews', 'action' => 'index', $topographic_map_id));
            } else {
                $this->Session->setFlash('Error guardando datos');
            }
        }
    }

    public function edit($id) {
        $this->TopographicMapReview->recursive = -1;
        if (!empty($this->data)) {

            if ($this->TopographicMapReview->saveAll($this->data)) {

                if ($this->data['TopographicMapReview']['archivo']['tmp_name'] != "") {
                    $property_id = $this->TopographicMapReview->TopographicMap->field('TopographicMap.property_id', array('TopographicMap.id' => $this->data['TopographicMapReview']['topographic_map_id']));
                    $proyect_id = $this->TopographicMapReview->TopographicMap->Property->field('Property.proyect_id', array('Property.id' => $property_id));
                    $codigo_proyecto = $this->TopographicMapReview->TopographicMap->Property->Proyect->field('Proyect.codigo', array('Proyect.id' => $proyect_id));
                    $nombreArchivo = $codigo_proyecto . "-RevisionPlanos-" . $this->data['TopographicMapReview']['id'] . ".pdf";
                    $rutaArchivo = APP . "webroot" . "/" . "files" . "/" . $codigo_proyecto . "/" . "revision_planos";

                    if (!is_dir($rutaArchivo)) {
                        if (!mkdir($rutaArchivo)) {
                            echo "error creando archivo";
                            //redirect
                        }
                    }
                    $rutaArchivo.= "/" . $nombreArchivo;
                    if (move_uploaded_file($this->data['TopographicMapReview']['archivo']['tmp_name'], $rutaArchivo)) {
                        $this->Session->setFlash('Archivo cargado con éxito', 'flash_custom');
                        $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
                    } else {
                        $this->Session->setFlash('No es posible cargar el archivo', 'flash_custom');
                        $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
                    }
                }

                $this->Session->setFlash('Registro guardado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
            } else {
                $this->Session->setFlash('Error guardando datos', 'flash_custom');
            }
        } else {
            $this->data = $this->TopographicMapReview->find('first', array('conditions' => array('TopographicMapReview.id' => $id), 'fields' => array('TopographicMapReview.*')));
        }
    }

    public function delete($id, $topographic_map_id) {
        if ($this->TopographicMapReview->delete($id)) {
            $this->Session->setFlash('Revisión borrada con éxito');
            $this->redirect(array('controller' => 'TopographicMapReviews', 'action' => 'index', $topographic_map_id));
        }
    }

}

?>
